# -*- coding: utf-8 -*-
"""
   File Name：     api.py
   Description :  Instantiate OCR, FaceRecognition from ocr.py, faceRecognition.py. Give a clear API for user.
   Author :       KangJiaHui
   date：         2020/02/07
"""

import json
from module.ocr import OCR
from module.utils.error import FaceNum, NameRecFail, NameMatchFail

ocr = OCR()
print("OCR created!")


def ocr_recog_one(image_base64, face_flag=False):
    """
    Extract information from an image.
    :param paper_name: str, the name of certificate. e.x. "道路危险货物运输驾驶员证"
    :param face_flag: bool, if True, extract face feature from input image.
    :param image_base64: image encoded in base64
    :return: json,
        e.x. {"result": 0, "message": "SUCCESS",
        "ocr": {"证件名": "中华人民共和国道路运输证", "有效期": null, "经营范围": "危险化学品",
        "车牌": "鲁FBR932", "人脸": [128dim_list], "人脸位置": [top, left, right, bottom]}}
    """
    try:
        result = ocr.recog_one(image_base64)
        result_json = json.dumps({"result": 0, "message": "SUCCESS", "ocr": result}, ensure_ascii=False)
    except NameRecFail as e:
        msg = str(e)
        result_json = json.dumps({"result": -4, "message": msg})
    except NameMatchFail as e:
        msg = str(e)
        result_json = json.dumps({"result": -3, "message": msg})
    except FaceNum as e:
        msg = str(e)
        result_json = json.dumps({"result": -2, "message": msg})
    except Exception as e:
        msg = str(e)
        result_json = json.dumps({"result": -1, "message": msg})
    return result_json


def face_register(image_base64):
    """
    get face feature from certificate image, stored in ocr.face_feature
    :param image_base64: image encoded in base64
    :return: json, e.x. {{"result": 0, "message": "SUCCESS", "feature":[[128dim_list], [top, left, right, bottom]]}
    """
    try:
        feature = ocr.face_register(image_base64)
        result_json = json.dumps({"result": 0, "message": "SUCCESS", "feature": feature})
    except FaceNum as e:
        msg = str(e)
        result_json = json.dumps({"result": -2, "message": msg})
    except Exception as e:
        msg = str(e)
        result_json = json.dumps({"result": -1, "message": msg})
    return result_json


def match_face(image_base64, face_feature):
    """
    Match face in input image with the faces in all certificates. Once a certificate matched, return True.
    :param face_feature: list, the feature vector to be matched.
    :param image_base64: image encoded in base64
    :return:json:
        e.x. {"result": 0, "message": "SUCCESS", "flag": true}
    """
    try:
        flag = ocr.face_match(image_base64, face_feature)
        result_json = json.dumps({"result": 0, "message": "SUCCESS", "flag": flag})
    except FaceNum as e:
        msg = str(e)
        result_json = json.dumps({"result": -2, "message": msg})
    except Exception as e:
        msg = str(e)
        result_json = json.dumps({"result": -1, "message": msg})
    return result_json


def car_plate_extract(image_base64):
    """
    Match car plate in input image with all car plates in certificates. Once a certificate matched, return True.
    :param image_base64: image encoded in base64
    :return: json, there may have more than one plate in one image.
    e.x. {"result": 0, "message": "SUCCESS",
    "plates": [["鲁FBR932", [[10,11], [25,11], [25,20], [10,20]]], ["鲁ABC123", [[20,21], [35,21], [35,30], [20,30]]]]}
    """
    try:
        plates = ocr.car_plate_extract(image_base64)
        result_json = json.dumps({"result": 0, "message": "SUCCESS", "plates": plates}, ensure_ascii=False)
    except Exception as e:
        msg = str(e)
        result_json = json.dumps({"result": -1, "message": msg})
    return result_json

